{include file="public/header"/}
<link rel="stylesheet" href="{__PUBLIC_PATH}/admin/pcf/pcfbase.css?v={$version}"/>
<script src="{__PUBLIC_PATH}/common/js/jquery.js?v={$version}"></script>
<script src="{__PUBLIC_PATH}/admin/pcf/global.js?v={$version}"></script>
</head>
<body>
<style type="text/css" media="screen">
.arctype-btn-expand{float:left;margin:3px 6px 0 0;display:block;width:14px;height:14px;background:url(/common/images/ico_plus.png) center center;}
.arctype-btn-fold{float:left;margin:3px 6px 0 0;display:block;width:14px;height:14px;background:url(/common/images/ico_minus.png) center center;}   
</style>
<div class="layui-fluid" id="LAY-component-layer-list">
    <div class="layui-card">
        <div class="layui-card-header" style="padding:10px 15px;">
            <div class="layui-btn-group">
                <a data-type="arctype_add" class="layui-btn layui-btn-normal" data-title="新增顶级栏目">新增顶级栏目</a>
            </div>
        </div>
        <div class="layui-card-body">
            <table class="layui-table" id="table1" lay-filter="table1"></table>
        </div>
    </div>
</div>
<script type="text/html" id="switchTpl">
    <input type="checkbox" name="is_hidden" data-id="{{d.id}}" value="{{d.is_hidden}}" lay-skin="switch" lay-text="正常|停用" lay-filter="is_hidden" {{ d.is_hidden == 1 ? 'checked' : ''}}>
</script>
<script type="text/html" id="oper-col">
    <a class="layui-btn layui-btn-xs" data-type="arctype_edit" data-id="{{d.id}}">编辑</a>
    <a class="layui-btn layui-btn-xs" data-type="arctype_detail" data-id="{{d.id}}" data-id="{{d.id}}" data-current="{{d.current_channel}}">内容</a>
    <a class="layui-btn layui-btn-xs" data-type="arctype_preview" data-url="{{d.typeurl}}">预览</a>
    {{# if(d.grade < {$arctype_max_level - 1}){ }}
    <a class="layui-btn layui-btn-danger layui-btn-xs" data-type="arctype_addson" data-id="{{d.id}}">增加子菜单</a>
    {{# }else{ }}
    <a class="layui-btn layui-btn-danger layui-btn-xs">不支持增加</a>
    {{# } }}
    <a class="layui-btn layui-btn-danger layui-btn-xs" data-type="arctype_del" data-url="{:url('/arctype/pseudo_del')}" data-title="{{d.typename}}" data-id="{{d.id}}">删除</a>
</script>
{include file="public/footer"/}
<script>
    var json_channeltype_list = {$json_channeltype_list|raw};
    layui.use(['layer', 'form', 'treeTable'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var treetable = layui.treeTable;
        var tbDataList = [];
        // 渲染表格
        var insTb = treetable.render({
            elem: '#table1',
            tree: {
                iconIndex: 1,
                idName: 'id',
                pidName: 'parent_id',
                isPidData: true
            },
            cellMinWidth: 100,
            cols: [
                {field: 'id', title: 'ID', width: 80},
                {field: 'typename', title: '<a href="javascript:;" class="arctype-btn-expand" id="btn-expand"></a><a href="javascript:;" class="arctype-btn-fold" id="btn-fold" style="display:none;"></a>栏目名称', minWidth: 300},
                {field: 'channeltypename', title: '所属模型', width: 150,align:'center'},
                {field: 'is_hidden', title: '状态', width: 120, align:'center',templet: '#switchTpl', unresize: true},
                {field: 'sort_order', title: '排序', width: 80,align:'center',edit:'text'},
                {title: '操作', templet: '#oper-col', singleLine: false, align: 'center', width: 360}
            ],
            reqData: function (data, callback) {
                $.post("{:url('/arctype/index')}", function (res) {
                    tbDataList = res.data ? res.data : [];
                    callback(res.data);
                }, 'json');
            }
        });
        $('#btn-expand').click(function () {
            $('#btn-expand').hide();
            $('#btn-fold').show();
            insTb.expandAll('#table1');
        });
        $('#btn-fold').click(function () {
            $('#btn-expand').show();
            $('#btn-fold').hide();
            insTb.foldAll('#table1');
        });
        $(document).on("click","#LAY-component-layer-list .layui-btn",function(){
          var type = $(this).data('type');
          active[type] && active[type].call(this);
        });
        //触发事件
        var active = {
            arctype_add: function(){
                layer.open({
                    type: 2,
                    title: '添加分类',
                    fixed: true,
                    shadeClose: false,
                    shade: 0.3,
                    area: ['100%', '100%'],
                    content: '{:url("/arctype/add")}'
                });
            },
            arctype_detail:function(){
                var obj = this;
                var id = $(obj).attr('data-id');
                var current_channel = $(obj).attr('data-current');
                if (current_channel == 6){
                    //单页内容
                    window.location.href= '{:url("/Arctype/single_edit")}?typeid='+id+"&goback=1";
                }else{
                    // 系统和自定义模型查看内容
                    var ifsystem = json_channeltype_list[current_channel]['ifsystem'];
                    var ctl_name = json_channeltype_list[current_channel]['ctl_name'];
                    if (ifsystem == 0) {
                        window.location.href= '{:url("/Custom/index")}?channel='+current_channel+"&typeid="+id+'&goback=1';
                    } else {
                        window.location.href= '{:url("/'+ctl_name+'/index")}?typeid='+id+"&goback=1";
                    }
                }
            },
            arctype_addson: function(){
                var obj = this;
                var id = $(obj).attr('data-id');
                layer.open({
                    type: 2,
                    title: '添加子分类',
                    fixed: true,
                    shadeClose: false,
                    shade: 0.3,
                    area: ['100%', '100%'],
                    content: '{:url("/arctype/add")}?parent_id='+id
                });
            },
            arctype_edit: function(){
                var obj = this;
                var id = $(obj).attr('data-id');
                layer.open({
                    type: 2,
                    title: '修改分类',
                    fixed: true,
                    shadeClose: false,
                    shade: 0.3,
                    area: ['100%', '100%'],
                    content: '{:url("/arctype/edit")}?id='+id
                });
            },
            arctype_preview: function(){
                var obj = this;
                var typeurl = $(obj).attr('data-url');
                var newWeb = window.open('_blank');
                newWeb.location = typeurl;
            },
            arctype_del: function(){
                var obj = this;
                var title = $(obj).attr('data-typename');
                var id = $(obj).attr('data-id');
                var typeurl = $(obj).attr('data-url');
                layer.confirm('<font color="#ff0000">如有子栏目及文档将一起清空</font>，确认删除到回收站？', {
                    title: false,
                    btn: ['确定','取消']
                }, function(){
                    layer.load(2);
                    $.ajax({
                        type : 'post',
                        url : typeurl,
                        data : {del_id:id},
                        dataType : 'json',
                        success : function(data){
                            layer.closeAll();
                            if(data.status){
                                layer.msg(data.msg, {icon: 1, time: 1000}, function(){
                                    window.location.reload();
                                });
                            }else{
                                layer.msg(data.msg, {icon: 2, time: 1000});
                            }
                        }
                    })
                }, function(index){
                    layer.close(index);
                });
                return false;
            }
        }
        //开启关闭
        form.on('switch(is_hidden)', function(){
            if ($(this).val() == 0){
                $(this).val(1);
            }else{
                $(this).val(0);
            }
            var dataid = $(this).attr('data-id');
            var pcfurl = '{:url("/index/changeTableVal")}';
            changeTableVal('arctype','id',dataid,'is_hidden',this,pcfurl,'arctype/index');
        });
    });
</script>
</body>
</html>